package com.qmzy.lab05.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qmzy.lab05.entity.Account;
import com.qmzy.lab05.entity.St;
import com.qmzy.lab05.entity.StAccount;
import com.qmzy.lab05.service.IAccountService;
import com.qmzy.lab05.service.IStService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjy
 * @since 2021-06-14
 */
@RestController
@RequestMapping("api/st")
public class StController {

    private static List<StAccount> stList;

    @Autowired
    private IStService stService;

    @Autowired
    private IAccountService service;

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/list")
    public List<StAccount> stList(){
        stList = stService.getListByAcc();
        return stList;
    }

    @ApiOperation("导出Excel")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StAccount.class).sheet("用户").doWrite(stList);
    }

    @PreAuthorize("hasRole('USER')")
    @GetMapping("/get")
    public St getOne(@RequestParam Integer id){
        St st = stService.getById(id);
        if(st == null){
            st = new St();
            st.setId(id);
        }
        return st;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/addAcc")
    public List<StAccount> addAccount(){
        Account account = new Account();
        service.save(account);
        stList = stService.getListByAcc();
        return stList;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/riskList")
    public List<StAccount> getRiskUser(){
        stList = stService.getListByYqdk();
        return stList;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/NoClockInList")
    public List<StAccount> getListNoClockIn(){
        stList = stService.getListNoClockIn();
        return stList;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/countrisk")
    public Integer getCountRisk(){
        return stService.getRiskCount();
    }

    @PreAuthorize("hasRole('USER')")
    @PostMapping("/update")
    public void updateSt(@RequestBody St st){
        if (stService.getById(st.getId())==null){
            stService.save(st);
        }else {
            stService.updateById(st);
        }
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/unclock")
    public List<StAccount> unClock(@RequestParam Integer id){
        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<Account>()
                .set("aclock",0)
                .eq("id",id);
        service.update(updateWrapper);
        stList = stService.getListByAcc();
        return stList;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/doclock")
    public List<StAccount> doClock(@RequestParam Integer id){
        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<Account>()
                .set("aclock",1)
                .eq("id",id);
        service.update(updateWrapper);
        stList = stService.getListByAcc();
        return stList;
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/delete")
    @Transactional
    public List<StAccount> deleteStAcc(@RequestParam Integer id){
        stService.removeById(id);
        service.removeById(id);
        stList = stService.getListByAcc();
        return stList;
    }

}
